package yang.yu.sunny.domain.sales;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 订单项目工具类
 * Created by yyang on 16/4/12.
 */
public class OrderItems {

    private List<OrderItem> items = new ArrayList<>();

    private OrderItems(List<OrderItem> items) {
        this.items = items;
    }

    public static OrderItems of(List<OrderItem> items) {
        return new OrderItems(items);
    }

    public List<Long> orders() {
        return extract(OrderItem::getOrderId);
    }

    public List<Long> products() {
        return extract(OrderItem::getProductId);
    }

    public List<Long> categories() {
        return extract(OrderItem::getCategoryId);
    }

    private List<Long> extract(Function<OrderItem, Long> function) {
        return items.stream().map(function).distinct().collect(Collectors.toList());
    }
}
